package cn.edu.zafu.bigdata.XuShuXin.Exp1203;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author: aluvfy
 * @description: 客户端，发送消息
 * @date: 2024/12/3 9:59
 */
public class TCPClient_chat_xsx {
    public static void main(String[] args) throws IOException {
        String hostName = "localhost"; // 服务端的IP地址,可以修改ip，进行访问交流
        int portNumber = 12345; // 服务端的端口号

        try (
                Socket echoSocket = new Socket(hostName, portNumber);
                PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
                BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))
        ) {
            String userInput;
            System.out.println("请输入：");
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput); // 发送消息给服务端
                System.out.println("Server: " + in.readLine()); // 打印服务端的回复
                System.out.println("请输入：");
            }

        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " + hostName);
            System.exit(1);
        }
    }
}